package cn.shop.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.shop.admin.service.InboxService;
import cn.shop.admin.service.OrderService;
import cn.shop.admin.service.message.MessageUtil;
import cn.shop.base.MyPage;
import cn.shop.msgs.OrderMsg;
import cn.shop.transcation.inbox.model.Inbox;
import cn.shop.transcation.order.entity.Order;
import cn.shop.transcation.order.entity.OrderQueryReq;
import cn.shop.transcation.order.mapper.OrderMapper;
import cn.shop.utils.enums.OrderStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jzw
 * @since 2021-11-30 09:47
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

	@Resource
	private MessageUtil messageUtil;
	@Resource
	private InboxService inboxService;

	@Override
	public MyPage<Order> orderList(MyPage<Order> page, OrderQueryReq req) {
		return baseMapper.pageWithMerchantInfo(page, req);
	}

	@Override
	public Map<String, Object> statistics(OrderQueryReq query) {
		return baseMapper.statisticsBack(query);
	}

	/**
	 * 退回订单
	 *
	 * @param ids 订单id
	 * @return
	 * @author jzw
	 * @since 2021/12/9 14:18
	 */
	@Override
	public boolean giveBack(List<Long> ids) {
		LambdaQueryWrapper<Order> query = Wrappers.lambdaQuery();
		query.in(Order::getId, ids).eq(Order::getStatus, OrderStatus.AUDITING);
		List<Order> list = baseMapper.selectList(query);
		Assert.isFalse(list.size() == ids.size(), "订单中有非公示期订单,无法进行退回");
		Order order = new Order();
		order.setStatus(OrderStatus.REJECTED);


		boolean b = SqlHelper.retBool(baseMapper.update(order, Wrappers.<Order>lambdaQuery()
				.in(Order::getId, ids)));
		if (b) {
			//发送mq 退回订单
			OrderMsg orderMsg = new OrderMsg();
			orderMsg.setIds(ids);
			orderMsg.setStatus(OrderStatus.REJECTED);
			orderMsg.setTime(LocalDateTime.now());
			try {
				messageUtil.sendMessage(OrderMsg.TOPIC, OrderMsg.TAG, orderMsg);
			} catch (Exception e) {
				log.warn("发送退回消息失败", e);
			}
			List<Inbox> inboxes = list.stream().map(o -> {
				Inbox inbox = new Inbox();
				inbox.setContent("您定订单审核未通过，请到游戏里领取退回的物品");
				inbox.setUserId(o.getSellerId());
				inbox.setCreateId(0L);
				return inbox;
			}).collect(Collectors.toList());
			if (CollectionUtil.isNotEmpty(inboxes)) {
				inboxService.saveBatch(inboxes, inboxes.size());
			}
		}
		return b;
	}

	@Override
	public boolean changeStatus(List<Long> ids, OrderStatus status) {
		Order update = new Order();
		update.setStatus(status);
		return update(update, Wrappers.<Order>lambdaQuery().in(Order::getId, ids));
	}

}
